<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div style="margin-top: 15px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form :inline="true">
            <el-form-item label="关键字">
              <el-input
                type="text"
                width="100"
                placeholder="关键字"
                v-model="searchObj.keyword"
                clearable
              />
            </el-form-item>

            <el-button type="primary" icon="el-icon-search" @click="fetchData()"
              >查询</el-button
            >
            <el-button type="default" @click="resetData">清空</el-button>
          </el-form>
        </el-col>
        <el-col :span="4" :offset="12">
          <el-button type="success" @click="add()">添 加</el-button>
          <el-button
            type="danger"
            @click="removeRows()"
            :disabled="multipleSelection.length === 0"
            style="margin: 0 10px"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
    </div>
    <!-- banner列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="海报图片" width="400rem" align="center">
        <template slot-scope="scope">
          <img style="height: 80px" :src="scope.row.imgUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="显示排序" />
      <el-table-column prop="remark" label="海报介绍" />
      <el-table-column label="是否上架" width="300px">
        <template slot-scope="scope">
          <p>
            <el-switch
              @change="handleStatusChange(scope.row)"
              :inactive-value="0"
              :active-value="1"
              active-text="是"
              inactive-text="否"
              v-model="scope.row.publishStatus"
            >
            </el-switch>
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <router-link :to="'/show/carouselInfo/edit/' + scope.row.id">
            <el-button type="text" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button
            type="text"
            style="margin: 0 20px"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import carouselInfoApi from '@/api/show/carouselInfo'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      multipleSelection: [] // 批量选择中选择的记录列表
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },

    add() {
      this.$router.push({ path: '/show/carouselInfo/add' })
    },

    // 加载banner列表数据
    fetchData(page = 1) {
      console.log('翻页。。。' + page)
      // 异步获取远程数据（ajax）
      this.page = page

      carouselInfoApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.records
          this.total = response.data.total

          // 数据加载并绑定成功
          this.listLoading = false
        })
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // promise
          // 点击确定，远程调用ajax
          return carouselInfoApi.removeById(id)
        })
        .then((response) => {
          this.fetchData(this.page)
          if (response.code) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      console.log('handleSelectionChange......')
      console.log(selection)
      this.multipleSelection = selection
    },

    // 批量删除
    removeRows() {
      console.log('removeRows......')

      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录!'
        })
        return
      }

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // promise
          // 点击确定，远程调用ajax
          // 遍历selection，将id取出放入id列表
          var idList = []
          this.multipleSelection.forEach((item) => {
            idList.push(item.id)
            // console.log(idList)
          })
          // 调用api
          return carouselInfoApi.removeRows(idList)
        })
        .then((response) => {
          this.fetchData(this.page)
          if (response.code) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleStatusChange(row) {
      carouselInfoApi.publish(row.id, row.publishStatus).then((response) => {
        this.$message({
          type: 'info',
          message: '操作成功'
        })
        this.fetchData()
      })
    }
  }
}
</script>
